﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using App.Utilities.Web.Handlers;
using CRMBiz;
using Newtonsoft.Json;
using CRMWeb.Utilities.Context;
using System.Net;
using Newtonsoft.Json.Linq;
using CRMFramework;

namespace CRMWeb.WebServices
{
    /// <summary>
    /// Summary description for EmailAccountSetting
    /// </summary>
    public class EmailAccountSetting : BaseHandler
    {
        //Bind Email Accounts
        public void BindEmailAccounts()
        {
            try
            {
                string strJson = JsonConvert.SerializeObject(EmailSettingHandler.BindEmailAccounts());
                context.Response.Write(strJson);
            }
            catch (Exception)
            {
                throw;
            }
        }

        //Get email account data
        public void GetEmailAccountData()
        {
            try
            {
                var pageIndex = string.IsNullOrEmpty(context.Request["page"]) ? 1 : int.Parse(context.Request["page"]);
                var pageSize = string.IsNullOrEmpty(context.Request["rows"]) ? 10 : int.Parse(context.Request["rows"]);
                DataSet ds = EmailSettingHandler.GetEmailAccountData(pageIndex, pageSize);
                var total = (ds.Tables[0].Rows.Count > 0) ? Convert.ToInt32(ds.Tables[0].Rows[0]["Total"]) : 0;
                string strJson = JsonConvert.SerializeObject(ds.Tables[0]);
                strJson = "{\"total\":\"" + total.ToString() + "\",\"rows\":" + strJson + "}";

                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
        }

        //Add new setting
        public int AddSettings(string Company, string EmailAccount, int IsDefault)
        {
            int intReturnValue = 0;
            try
            {
                intReturnValue = EmailSettingHandler.AddSettings(Company, EmailAccount, IsDefault);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
            return intReturnValue;
        }


        //Edit Settings
        public int EditSettings(int ID, string Company, string EmailAccount, int IsDefault)
        {
            int intReturnValue = 0;
            try
            {
                intReturnValue = EmailSettingHandler.EditSettings(ID, Company, EmailAccount, IsDefault);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
            return intReturnValue;
        }

        //Delete setting:
        public bool DeleteSettings(int ID)
        {
            bool flag = false;
            try
            {
                flag = EmailSettingHandler.DeleteSettings(ID);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
            return flag;
        }

       
    }


}